﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.Entities.Seguridad;
using GC2.ExceptionHandler;

public partial class Stock_StockAdmin : GenericPage
{
    Usuarios usuario = new Usuarios();
    Stock stockActual = new Stock();    
    List<Stock> listaStock = new List<Stock>();
    private ExceptionConstructor exNew = new ExceptionConstructor();

    #region Controles

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                usuario = (Usuarios)Session["UsuarioActual"];
                rdbCompras.Checked = true;
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
   
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/Seguridad/Default.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            BMStock bmStock = new BMStock(Connection);
            Stock stock = new Stock();
            stock.Id_Rubro = Convert.ToInt16(Rubros1.CboRubro.SelectedValue);
            if (Rubros1.CboSubRubro.SelectedValue != "")
            {
                stock.Id_SubRubro = Convert.ToInt16(Rubros1.CboSubRubro.SelectedValue);
            }
            listaStock = bmStock.BMStockGetAll(stock);
            Session["listaStock"] = listaStock;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void gvStock_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvStock.PageIndex = e.NewPageIndex;
        CargarGrilla();
    }

    #region gvStock_RowDataBound
    /// <Summary>
    /// Boundeo de la grilla
    /// CBendayan 23/03/2010 
    /// </Summary>
    /// <returns></returns>
    protected void gvStock_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            CheckBox chkLote;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                chkLote = (CheckBox)e.Row.Cells[6].FindControl("chkLotes");
                if (listaStock[e.Row.RowIndex + (gvStock.PageIndex * 15)].UsaLotes == 1)
                {
                    chkLote.Checked = true;
                }
                else
                {
                    chkLote.Visible = false;
                }
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    protected void gvStock_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Stock stock = new Stock();
            stock.Id_Rubro = Convert.ToInt32(Rubros1.CboRubro.SelectedValue);
            stock.Id_SubRubro = Convert.ToInt32(Rubros1.CboSubRubro.SelectedValue);
            stock.Id_Articulo = (int)gvStock.SelectedDataKey.Value;
            listaStock = (List<Stock>)Session["listaStock"];
            foreach (Stock articulo in listaStock)
            {
                if (stock.Id_Articulo == articulo.Id_Articulo)
                {
                    stock.Articulo = articulo.Articulo;
                    break;
                }
            }

            Session["Stock"] = stock;
            if (rdbCompras.Checked)
            {
                Session["Tipo"] = "Compras";
            }
            if (rdbVentas.Checked)
            {
                Session["Tipo"] = "Ventas";
            }
            if (rdbBalance.Checked)
            {
                Session["Tipo"] = "Balance";
            }
            Response.Redirect("Articulo.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

    #region Métodos
    private void CargarGrilla()
    {
        try
        {
            listaStock = (List<Stock>)Session["listaStock"];
            gvStock.DataSource = listaStock;
            gvStock.DataBind();
        }
        catch (Exception ex)
        { throw ex; }
    }

    #endregion

    protected void rdbCompras_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbCompras.Checked)
        {
            rdbVentas.Checked = false;
            rdbBalance.Checked = false;
        }
    }
    protected void rdbVentas_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbVentas.Checked)
        {
            rdbCompras.Checked = false;
            rdbBalance.Checked = false;
        }
    }
    protected void rdbBalance_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbBalance.Checked)
        {
            rdbCompras.Checked = false;
            rdbVentas.Checked = false;
        }
    }
}
